package com.tank.collision;

import com.almasb.fxgl.core.math.FXGLMath;
import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.dsl.components.EffectComponent;
import com.almasb.fxgl.dsl.components.HealthIntComponent;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.physics.CollisionHandler;
import com.tank.TankApp;
import com.tank.components.BulletLevelComponent;
import com.tank.constant.GameType;
import com.tank.constant.ItemType;
import com.tank.effect.FreezeEffect;
import com.tank.effect.HelmetEffect;
import com.tank.effect.ShipEffect;

import java.util.List;

public class ItemEnemyCollideHandler extends CollisionHandler {
    public ItemEnemyCollideHandler() {
        super(GameType.ITEM, GameType.ENEMY);
    }

    @Override
    protected void onCollision(Entity item, Entity enemy) {
        ItemType itemType = item.getObject("itemType");
        switch (itemType) {
            case GUN -> enemy.getComponent(BulletLevelComponent.class).fullGrade();
            case STAR -> enemy.getComponent(BulletLevelComponent.class).upgrade();
            case TANK -> {
                List<Entity> players = FXGL.getGameWorld().getEntitiesByType(GameType.PLAYER);
                if (players.size() > 0) {
                    Entity player = FXGLMath.random(players).get();
                    player.getComponent(HealthIntComponent.class).damage(1);
                }
            }
            case HEART -> {
                List<Entity> players = FXGL.getGameWorld().getEntitiesByType(GameType.PLAYER);
                if (players.size() > 0) {
                    Entity player = FXGLMath.random(players).get();
                    player.getComponent(HealthIntComponent.class).damagePercentageCurrent(50);
                }
            }
            case BOMB -> {}
            case SHIP -> enemy.getComponent(EffectComponent.class).startEffect(new ShipEffect());
            case TIME -> {
                List<Entity> players = FXGL.getGameWorld().getEntitiesByType(GameType.PLAYER);
                for (Entity player : players) {
                    player.getComponent(EffectComponent.class).startEffect(new FreezeEffect());
                }
            }
            case HELMET -> enemy.getComponent(EffectComponent.class).startEffect(new HelmetEffect());
            case SPADE -> FXGL.<TankApp>getAppCast().destroyBaseWall();
        }
        item.removeFromWorld();
        FXGL.play("item.wav");
    }
}
